﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Tireguit.Nmon.Parser;
using System.Threading;

namespace Tireguit.Nmon.UI.Elements.Controls
{
    public partial class ParseControl : UserControl
    {
        public IDbConnection Connection { get; set; }
        protected Thread _workerthread;
        protected System.Timers.Timer _timer = null;
        public ParseControl(IDbConnection connection)
        {
            Connection = connection;
            InitializeComponent();
        }

        private void Browse_Click(object sender, EventArgs e)
        {
          //  openFileDialog1.InitialDirectory=this.f
            openFileDialog1.FileName = string.Empty;
            openFileDialog1.CheckFileExists = true;
            openFileDialog1.Multiselect = false;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
                textBox1.Text = openFileDialog1.FileName;

        }

        private void Cancel_Click(object sender, EventArgs e)
        {

        }
        private void Loadfile()
        {
            FileParser fileparse = null;
            try
            {
                string filename = textBox1.Text.Trim();
                System.IO.File.Exists(filename);
                fileparse = new FileParser(filename, Connection);
                fileparse.DoParsing();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Chargement de fichier", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            	
            }
            finally
            {
                if (fileparse!=null)
                {
                    fileparse.Dispose();
                }
            }

        }
        private  void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
        {
            progressBar1.Value++;
            progressBar1.PerformStep();
           // this.Refresh();
        }


        private void Load_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                this.Load.Enabled = false;
                this.textBox1.Enabled = false;
                this.Browse.Enabled = false;
                _workerthread = new Thread(Loadfile);
                _timer = new System.Timers.Timer();
                _timer.Interval = TimeSpan.FromSeconds(10).TotalMilliseconds;
                _timer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);
                progressBar1.Value = 1;
                progressBar1.Step = 2;
                progressBar1.Enabled = true;
                _workerthread.Start();
                _timer.Start();
                _workerthread.Join();
                _timer.Stop();

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Chargement de fichier", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            	
            }
            finally
            {
                if (_timer!=null)
                {
                    _timer.Dispose();
                    _timer = null;
                }
                this.Load.Enabled = true;
                this.textBox1.Enabled = true;
                this.Browse.Enabled = true;
                
                Cursor.Current = Cursors.Default;
            }
            

           
        }
    }
}
